package org.example.business.feign.fallback;

import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.tm.api.GlobalTransactionContext;
import lombok.extern.slf4j.Slf4j;
import org.example.business.feign.GoodsFeign;
import org.example.entity.Goods;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class GoodsFeignFallback implements GoodsFeign {
    @Override
    public Goods queryById(Long id) {
        return null;
    }

    @Override
    public String reduceGoods(Long id, Integer amount) {
        log.info("reduce goods fallback");
        if (RootContext.inGlobalTransaction()) {
            try {
                GlobalTransactionContext.reload(RootContext.getXID()).rollback();
            } catch (TransactionException e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }
}
